//
//  RZQReplayViewController.m
//  LastOne
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQReplayViewController.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "ThinkModel.h"
#import "FitHeader.h"
#import "RZQReplayTableViewCell.h"
#import "VoteView.h"
#import "UILabel+ThemeChange.h"
#import <UIImageView+WebCache.h>
#import "RecommendModel.h"
#import "NightHeader.h"
@interface RZQReplayViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIImageView *image;
@property(nonatomic, strong)NSMutableArray *commondArray;
@end

@implementation RZQReplayViewController
#pragma mark 网络解析
-(void)getNet
{
    _dataSource = [NSMutableArray array];
    [HTTPMethod getDataByString:@"http://app.qdaily.com/app/papers/done" BodyDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"options=%ld&paper_id=%ld", (long)_netData.Id ,  (long)_cirData.iD]] WithDataBlock:^(id data) {
       
        NSDictionary *dic = [data objectForKey:@"response"];
        NSArray *array = [dic objectForKey:@"everyones_attitude"];
        for (NSDictionary *dic in array) {
            ThinkModel *model = [[ThinkModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:model];
        }
       
        [_tableView reloadData];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, _dataSource.count * 70 * FitHeight + 120 * FitHeight )];
        
        UILabel *headLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, 10 * FitHeight, 200 * FitWidth, 40 * FitHeight)];
        headLabel.text = @"大家的态度";
        headLabel.font = [UIFont systemFontOfSize:30];
        [headLabel NightWithType:UIViewColorTypeNormal];
        [_tableView.tableHeaderView addSubview:headLabel];         
        
        for (NSInteger i = 0; i < _dataSource.count; i++) {
            VoteView *voteView = [[VoteView alloc]initWithFrame:CGRectMake(0, i * 70 * FitHeight + 60 * FitHeight, self.view.frame.size.width, 70 * FitHeight)];
            voteView.messageLabel.text = [_dataSource[i]content];
            voteView.percentLabel.text = [NSString stringWithFormat:@"%ld%%", [_dataSource[i] percent]];
            
            [voteView.percentLabel drawRect:voteView.percentLabel.frame];
            [_tableView.tableHeaderView addSubview: voteView];
            [voteView.circleLabel drawRect:voteView.circleLabel.frame];
            voteView.x = 0;
            voteView.y = [_dataSource[i] percent] ;
            [voteView NightWithType:UIViewColorTypeNormal];
            
        }
        UILabel *lastLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 * FitWidth, _dataSource.count * 70 * FitHeight + 70 * FitHeight, 200 * FitWidth, 40 * FitHeight)];
       
        lastLabel.text = [NSString stringWithFormat:@"%ld条评论",  _cirData.comment_count];
        lastLabel.font = [UIFont systemFontOfSize:30];
        [_tableView.tableHeaderView addSubview:lastLabel];
        [lastLabel NightWithType:UIViewColorTypeNormal];
        
       
    }];
    _commondArray = [NSMutableArray array ];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://app.qdaily.com/app/comments/index/paper/%ld/0.json", _cirData.iD] BodyDic:nil WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"response"];
        NSDictionary *Dic = [dic objectForKey:@"comments"];
        NSArray *array = [Dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            RecommendModel *model = [[RecommendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_commondArray addObject:model];
            
        }
        [_tableView reloadData];
         
    }];
    
}

-(void)loadView
{
    [super loadView];
     [self getNet];
    
    _tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 * FitHeight)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_tableView]; 
    _image = [[UIImageView alloc]initWithFrame:CGRectMake(0, -200 * FitHeight, ScreenWidth, 200 * FitHeight)];
    _tableView.contentInset = UIEdgeInsetsMake(200 * FitHeight, 0, 0, 0);
    [_image sd_setImageWithURL:[NSURL URLWithString:_cirData.image]];
    
    _image.backgroundColor = [UIColor redColor];
    _image.contentMode = UIViewContentModeScaleAspectFill;
    
    [_tableView addSubview:_image];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
 
    
    
    
    // Do any additional setup after loading the view.
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"cell";
    RZQReplayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[RZQReplayTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.modelData = _commondArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.high = [self getHeight:[_commondArray[indexPath.row]content] With:20];
    
    return cell;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return   _commondArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat high = [self getHeight:[_commondArray[indexPath.row]content] With:20];
        return 100 + high;
    
}
#pragma mark 自适应高度
-(CGFloat)getHeight:(NSString *)string With:(NSInteger)font
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil];
    
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(300 * FitWidth , CGFLOAT_MAX * FitHeight ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
